import Vue from 'vue'
import VueRouter from 'vue-router'
// import Main from '../views/Main.vue'
// import home from '../views/home/index.vue'
// import user from '../views/user/index.vue'
Vue.use(VueRouter)
const routes = [
  {
    path: '/login',
    name: 'Login',
    // 路由懒加载
    component: () => import('../views/Login/Login.vue')
  },

  {
    path: '/',
    component: () => import('../views/CommonMain.vue'),
    //路由元信息
    meta: {
      isLogin: true
    },
    children: [
      {
        path: '/qys',
        name: 'QuanYaoSu',
        component: () => import('../views/QuanYaoSu/QuanYaoSu.vue')
      },
      {
        path: '/zc',
        name: 'ZongChang',
        component: () => import('../views/ZongChang/ZongChang.vue')
      },
      {
        path: '/pj',
        name: 'PianJiao',
        component: () => import('../views/PianJiao/PianJiao.vue')
      },
      {
        path: '/qj',
        name: 'QingJiao',
        component: () => import('../views/QingJiao/QingJiao.vue')
      },
      {
        path: '/xfl',
        name: 'XFenLiang',
        component: () => import('../views/XFenLiang/XFenLiang.vue')
      },
      {
        path: '/yfl',
        name: 'YFenLiang',
        component: () => import('../views/YFenLiang/YFenLiang.vue')
      },
      {
        path: '/zfl',
        name: 'ZFenLiang',
        component: () => import('../views/ZFenLiang/ZFenLiang.vue')
      }
    ]
  },

  {
    path: '/selectinstrument',
    name: 'SelectInstrument',
    component: () => import('../views/SelectInstrument/SelectInstrument.vue')
  }
]
//路由配置
const router = new VueRouter({
  //路由模式
  // 访问VUE的项目是会在访问地址后面加上#，这个#其实是VUE的HASH模式所产生的，正确点来说是因为VUE使用了HASH模式。
  // 当不需要#时，采用history模式即可
  mode: 'history',
  routes
})

//路由拦截
router.beforeEach((to, from, next) => {
  //1. 判断是否需要登录
  if (to.path === '/selectinstrument' || to.matched.some((ele) => ele.meta.isLogin)) {
    //2. 判断当前的用户是否已经登录
    let token = sessionStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

// const originalPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push(location, onResolve, onReject) {
//   if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
//   return originalPush.call(this, location).catch((err) => err)
// }
export default router
